<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\user\Follow;
use think\Db;

class Focus extends Api
{
    protected $noNeedRight = "*";
    protected $noNeedLogin = "";

    public function index()
    {
        $user_id = $this->request->param('user_id', 0);
        $status = $this->request->param('status', 1);
        $type = $this->request->param('type');
        if ($user_id == 0 || !$type) {
            $this->error("参数有误");
        }

        if ($user_id == $this->auth->id) {
            $this->success("操作成功");
        }
        $result = Follow::focus($this->auth->id, $user_id, $status, $type);

        if ($result) {
            $this->success("操作成功", $result);
        }
        $this->error("操作失败", $result);
    }

    public function nums()
    {
        $this->success("信息", Follow::nums($this->auth->id));
    }

    public function list()
    {
        $params = $this->request->param();

        $this->success('列表', Follow::list($params, $this->auth->id));
    }

    public function main()
    {
        $pagesize = $this->request->param('pagesize', 10);
//        $list = Db::name('user_follow')->alias('uf')
//            ->join('user u', 'u.id = uf.r_user_id')
//            ->join('anchor_config ac', 'ac.anchor_id = uf.r_user_id')
//            ->where("uf.l_user_id", $this->auth->id)->where('uf.genre', 1)
//            ->where("u.group_id", 1)->where('uf.status', 1)
//            ->field("u.nickname,u.avatar,ac.place_price,ac.gen_price,uf.r_user_id")
//            ->order("uf.updatetime DESC")->paginate($pagesize);

//        $list = Db::name('user_follow')->where('r_user_id', $this->auth->id)->where('status', 1)->where('genre', 1)
//            ->paginate($pagesize)->each(function ($v) {
//                $v['anchor_user'] = Db::name('user')->where('group_id', 1)->where('id', $v['l_user_id'])->find();
//                $v['anchor_user']['place_price'] = Db::name('anchor_config')->where('anchor_id', $v['l_user_id'])->value('place_price');
//                $v['anchor_user']['gen_price'] = Db::name('anchor_config')->where('anchor_id', $v['l_user_id'])->value('gen_price');
//                return $v;
//            });;
//                $this->success("成功", $list);
        //4.11
        $list = Db::name('user_follow')->where('r_user_id', $this->auth->id)->where('status', 1)->where('genre', 1)->select();
        $data =[];
        foreach ($list as $k=>$v){
            $anchor = Db::name('user')->where('id',$v['l_user_id'])->where('group_id',1)->find();
            if($anchor){
                $data[]=$v;
            }
        }

        $pageSize =$pagesize;  //分页每页条数
        $page =  1; //分页页数 默认为1
//        $order_merge = array_merge($array1,$array2);
        $a = $page-1;
        $b = $a * $pageSize;
        //时间排序数组
        $time_array = array();
        foreach ($data as $k => $v) {
            $data[$k]['anchor_user'] = Db::name('user')->where('group_id', 1)->where('id', $v['l_user_id'])->find();
            $data[$k]['anchor_user']['place_price'] = Db::name('anchor_config')->where('anchor_id', $v['l_user_id'])->value('place_price');
            $data[$k]['anchor_user']['gen_price'] = Db::name('anchor_config')->where('anchor_id', $v['l_user_id'])->value('gen_price');
        }
//        array_multisort($time_array, SORT_DESC, $order_merge);  //通过时间排序
        $data_rt['total'] = count($data); //获取总条数
        $data_rt['last_page'] = round($data_rt['total'] /$pageSize , 0);
        $order_merge = array_slice($data,$b,$pageSize); //分页部分
        $data_rt['data'] = $order_merge;
        $res = $data_rt;
        $this->success('成功',$res);
    }
}